<template>
  <div class="layout">
    <div class="sidebar">
      <div class="logo">
        <span class="logo-text">后台管理系统</span>
        <div class="logo-decoration"></div>
      </div>
      <nav class="menu">
        <router-link 
          v-for="item in menuItems" 
          :key="item.path"
          :to="item.path"
          class="menu-item"
          :class="{ active: currentPath === item.path }"
        >
          <div class="menu-icon">
            <i :class="item.icon"></i>
          </div>
          <span class="menu-text">{{ item.name }}</span>
        </router-link>
      </nav>
    </div>
    <div class="main">
      <div class="header">
        <div class="user-info">
          <div class="user-avatar">{{ userInfo.nickname?.[0] || userInfo.username?.[0] || 'U' }}</div>
          <span class="username">{{ userInfo.nickname || userInfo.username }}</span>
          <button class="logout-btn" @click="handleLogout">
            <span class="btn-text">退出登录</span>
            <span class="btn-icon">→</span>
          </button>
        </div>
      </div>
      <div class="content">
        <router-view v-slot="{ Component }">
          <transition name="fade-slide" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

const currentPath = computed(() => route.path)

const userInfo = ref(JSON.parse(localStorage.getItem('userInfo') || '{}'))

const menuItems = [
  { name: '用户管理', path: '/user', icon: 'icon-user' },
  { name: '商品管理', path: '/product', icon: 'icon-product' },
  { name: '门店管理', path: '/store', icon: 'icon-store' }
]

const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  router.push('/login')
}
</script>

<style scoped>
.layout {
  display: flex;
  height: 100vh;
  background-color: #f8faf9;
}

.sidebar {
  width: 240px;
  background: linear-gradient(180deg, #4caf50 0%, #43a047 100%);
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 4px 0 10px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 2;
}

.logo {
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.logo-text {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 1px;
  position: relative;
  z-index: 1;
}

.logo-decoration {
  position: absolute;
  width: 160%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  transform: skewX(-20deg);
  animation: shine 3s infinite;
}

@keyframes shine {
  from { transform: translateX(-100%) skewX(-20deg); }
  to { transform: translateX(100%) skewX(-20deg); }
}

.menu {
  padding: 20px 0;
  overflow-y: auto;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 12px 24px;
  color: rgba(255, 255, 255, 0.85);
  text-decoration: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  margin: 4px 12px;
  border-radius: 8px;
}

.menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
  transform: translateX(4px);
}

.menu-item.active {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
}

.menu-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  border-radius: 8px;
  transition: all 0.3s;
}

.menu-item.active .menu-icon {
  background-color: rgba(255, 255, 255, 0.1);
}

.menu-text {
  font-weight: 500;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
}

.header {
  height: 70px;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 24px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 1;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  background-color: #4caf50;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
}

.username {
  font-weight: 500;
  color: #333;
}

.logout-btn {
  padding: 8px 16px;
  border: none;
  background-color: #4caf50;
  color: white;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s;
  font-weight: 500;
}

.logout-btn:hover {
  background-color: #45a049;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.2);
}

.logout-btn:active {
  transform: translateY(0);
}

.btn-icon {
  transition: transform 0.3s;
}

.logout-btn:hover .btn-icon {
  transform: translateX(4px);
}

.content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
}

/* 页面切换动画 */
.fade-slide-enter-active,
.fade-slide-leave-active {
  transition: all 0.3s ease;
}

.fade-slide-enter-from {
  opacity: 0;
  transform: translateX(20px);
}

.fade-slide-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}

/* 图标样式 */
.icon-user::before { content: '👤'; font-size: 20px; }
.icon-product::before { content: '📦'; font-size: 20px; }
.icon-store::before { content: '🏪'; font-size: 20px; }
</style> 